package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.AdditionalDrawLotteryRuleDao;
import com.qdlc.p2p.dal.dao.AdditionalMovieTicketRuleDao;
import com.qdlc.p2p.dal.dao.ExperienceRuleDao;
import com.qdlc.p2p.dal.dao.NewActivityAwardLogDao;
import com.qdlc.p2p.dal.dao.RateRuleDao;
import com.qdlc.p2p.dal.dao.RedEnvelopeDao;
import com.qdlc.p2p.dal.dto.AdditionalDrawLotteryRule;
import com.qdlc.p2p.dal.dto.AdditionalMovieTicketRule;
import com.qdlc.p2p.dal.dto.ExperienceRule;
import com.qdlc.p2p.dal.dto.NewActivityAwardLog;
import com.qdlc.p2p.dal.dto.RateRule;
import com.qdlc.p2p.dal.dto.RedEnvelope;
import com.qdlc.p2p.dal.model.NewActivityAwardLogModel;

/**
 * Created by liaoxiangkai on 2015/11/12.
 */
@Repository("newActivityAwardLogDao")
public class NewActivityAwardLogDaoImpl extends BaseDaoImpl<NewActivityAwardLog> implements NewActivityAwardLogDao {

    /**
     * 查询赠送用户活动列表
     *
     * @param model
     * @return
     */
    @Override
    public PageDataList<NewActivityAwardLogModel> list(NewActivityAwardLogModel model) {
        QueryParam param = QueryParam.getInstance().addPage(model.getPage(), model.getRows());
        if (StringUtil.isNotBlank(model.getSearchName())) {
            param.addParam("user.userName", SearchFilter.Operators.LIKE, model.getSearchName());
        }
        PageDataList<NewActivityAwardLog> pageList = this.findPageList(param);
        PageDataList<NewActivityAwardLogModel> pageModelList = new PageDataList<NewActivityAwardLogModel>();
        List<NewActivityAwardLogModel> list = new ArrayList<NewActivityAwardLogModel>();
        if (null != pageList.getList() && pageList.getList().size() > 0) {
            for (NewActivityAwardLog entity : pageList.getList()) {
                NewActivityAwardLogModel logModel = NewActivityAwardLogModel.instance(entity);
                logModel.setUserName(entity.getUser().getUserName());
                if (null != entity.getBeInvitedUser())
                    logModel.setBeInvitedUserName(entity.getBeInvitedUser().getUserName());
                //1:红包 2:体验券 3:加息券 4:抽奖 5:电影票
                setRuleName(entity.getRuleType(), entity.getRuleId(), logModel);
                list.add(logModel);
            }
        }
        pageModelList.setPage(pageList.getPage());
        pageModelList.setList(list);
        return pageModelList;
    }

    /**
     * 给model设置规则名称
     *
     * @param ruleType
     * @param ruleId
     * @param model
     */
    private void setRuleName(int ruleType, long ruleId, NewActivityAwardLogModel model) {
        switch (ruleType) {
            case (1):
                RedEnvelopeDao redEnvelopeDao = (RedEnvelopeDao) BeanUtil.getBean("redEnvelopeDao");
                RedEnvelope redEnvelope = redEnvelopeDao.find(ruleId);
                if (null != redEnvelope)
                    model.setRuleName(redEnvelope.getName());
                break;
            case (2):
                ExperienceRuleDao experienceRuleDao = (ExperienceRuleDao) BeanUtil.getBean("experienceRuleDao");
                ExperienceRule experienceRule = experienceRuleDao.find(ruleId);
                if (null != experienceRule)
                    model.setRuleName(experienceRule.getName());
                break;
            case (3):
                RateRuleDao rateRuleDao = (RateRuleDao) BeanUtil.getBean("rateRuleDao");
                RateRule rateRule = rateRuleDao.find(ruleId);
                if (null != rateRule)
                    model.setRuleName(rateRule.getRuleName());
                break;
            case (4):
                AdditionalDrawLotteryRuleDao lotteryRuleDao = (AdditionalDrawLotteryRuleDao) BeanUtil.getBean("additionalDrawLotteryRuleDao");
                AdditionalDrawLotteryRule lotteryRule = lotteryRuleDao.find(ruleId);
                if (null != lotteryRule)
                    model.setRuleName(lotteryRule.getName());
                break;
            case (5):
                AdditionalMovieTicketRuleDao movieTicketRuleDao = (AdditionalMovieTicketRuleDao) BeanUtil.getBean("additionalMovieTicketRuleDao");
                AdditionalMovieTicketRule movieTicketRule = movieTicketRuleDao.find(ruleId);
                if (null != movieTicketRule)
                    model.setRuleName(movieTicketRule.getName());
                break;
        }
    }
}
